package com.education.basic.service.impl;

import com.education.basic.dao.AdministratorDao;
import com.education.basic.entity.Administrator;
import com.education.basic.service.AdministratorService;
import com.yunshangwoniu.core.Assert.UserCommonAssert;
import com.yunshangwoniu.core.baseService.PageService;
import com.yunshangwoniu.core.page.Page;
import com.yunshangwoniu.core.utils.DateUtils;
import com.yunshangwoniu.core.utils.MD5Utils;
import com.yunshangwoniu.core.utils.RequestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@Service("administratorService")
public class AdministratorServiceImpl extends BaseServiceImpl<Administrator> implements AdministratorService {
    private static final Logger LOGGER = LoggerFactory.getLogger(BaseServiceImpl.class);
    @Resource
    private AdministratorDao administratorDao;
    @Resource
    private PageService<Administrator> pageService;

    @Override
    public Administrator login(String username, String password) {
        Administrator ad = administratorDao.getByUsername(Administrator.class, username);
        if (ad != null) {
            ad = MD5Utils.MD5(password).equals(MD5Utils.JM(ad.getPassword())) ? ad : null;    //当用户名或者密码错误的时候直接返回null
        }
        if (ad != null) {
            ad.setLastAccess(System.currentTimeMillis());
            ad.setLastAccessStr(DateUtils.getCurrentTime());
            administratorDao.update(ad);                                                //如果登录成功则更新最后一次登录时间
        }

        return ad;
    }

    @Override
    public Administrator saveNewAdministrator(Administrator ad) {
        UserCommonAssert.notNull(ad, "系统异常");
        ad.setCreateTime(System.currentTimeMillis());
        ad.setCreateTimeStr(DateUtils.getCurrentTime());
        ad.setLastAccess(System.currentTimeMillis());
        ad.setLastAccessStr(DateUtils.getCurrentTime());
        ad.setPassword(MD5Utils.KL(MD5Utils.MD5(ad.getPassword())));        //用户密码采用MD5加密
        return administratorDao.save(ad);
    }

    @Override
    public Page<Administrator> queryAdministro(int currentPage, int pageSize, Map<String, Object> params,
                                               HttpServletRequest request) {
        Page<Administrator> page = new Page<Administrator>(currentPage, pageSize, RequestUtils.getPageUrl(request));
        page.setParams(params);
        page = administratorDao.query(page, Administrator.class);
        pageService.htmlBuilder(page, "default");
        return page;
    }

    @PostConstruct
    public void saveInitAdmin() {
        LOGGER.info("Staring init education system admin...");
        Administrator administrator = administratorDao.getByUsername(Administrator.class, "admin");
        if(administrator!=null) return;
        SimpleDateFormat format = new SimpleDateFormat("yy-MM-dd HH:mm:ss");
        Administrator initAd = new Administrator();
        initAd.setPassword("admin");
        initAd.setUsername("admin");
        initAd.setAddress("China shanghai");
        initAd.setEmail("10010@qq.com");
        initAd.setGender("男");
        initAd.setLevel(1);
        initAd.setName("Joke");
        initAd.setLastAccess(System.currentTimeMillis());
        initAd.setLastAccessStr(format.format(new Date()));
        initAd.setCreateTime(System.currentTimeMillis());
        initAd.setCreateTimeStr(format.format(new Date()));
        initAd.setTelphone("10086");
        initAd.setNote("Good body");
        initAd.setHeadUrl("");
        saveNewAdministrator(initAd);
        LOGGER.info("Education init success first admin is {}","admin");
    }
}
